define(function (require, exports, module) {
    var angular = require('angular');
    require("pagination");
    require("angular-ui");
    var util = require("util");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀

    angular.module('ParamsInfor2',[])
        .controller('paramsBalancerateCtrl',['$scope','$location', '$routeParams',function($scope,$location, $routeParams){

            $scope.params = [];
            $scope.keyword = '';

            $scope.ddlConfigApplicationName = {
                allowClear: true,
                data: [],
                placeholder: '应用名称'
            };

            $scope.ddlConfigBUName = {
                allowClear: true,
                data: [],
                placeholder: '区域名称'
            };

            //获取结算比例相关的信息
            $scope.getBalanceRateInfo = function () {
                var url = api_prefix + "/params/get-balance-rate-info";
                util.myAjax({
                    url: url,
                    type: "POST",
                    context: this,
                    data: [],
                    success: function (json) {
                        if (json.retCode === "0") {
                            $.each(json.data.applist, function (idx,item) {
                                $scope.ddlConfigApplicationName.data.push({id:item.balance_rate_id,text:item.appname});
                            });
                            $.each(json.data.bulist, function (idx,item) {
                                $scope.ddlConfigBUName.data.push({id:item.BUGUID,text:item.BUName});
                            });
                        } else {
                            util.alert("获取结算比例相关信息失败：" + json.errMsg);
                        }
                    }
                });
            };
            
            $scope.getBalanceRate = function () {
                var url = api_prefix + "/params/get-balance-rate";
                util.myAjax({
                    url: url,
                    type: "POST",
                    context: this,
                    data: {keyword:$scope.keyword},
                    success: function (json) {
                        if (json.retCode === "0") {
                            $scope.params = json.data;
                            $scope.$applyAsync();
                        } else {
                            util.alert("获取结算比例信息失败：" + json.errMsg);
                        }
                    }
                });
            }

            $scope.saveBalanceRate = function (record) {
                var url = api_prefix + "/params/save-params-balance-rate";

                var oid = record.application_bu_rate_id;
                var appid = record.balance_rate_id;
                var buid = record.BUGUID;
                var rate = record.rate;
                if(!appid || appid == ''){
                    util.alert('请选择产品!');
                    return;
                }
                if(!buid || buid == ''){
                    util.alert('请选择区域!');
                    return;
                }
                if(!rate || rate == ''){
                    util.alert('请填写比例!');
                    return;
                }
                var postdata = {
                    oid:oid,
                    appid:appid,
                    buid:buid,
                    rate:rate
                };

                util.myAjax({
                    url: url,
                    type: "POST",
                    data: {balancerate:postdata},
                    success: function (json) {
                        if (json.retCode === 0) {
                            util.alert("保存成功!");
                            $scope.getBalanceRate();
                        } else {
                            util.alert("保存失败：" + json.errMsg);
                        }
                    }
                });
            };

            $scope.newBalanceRate = function (record) {
                var items = $scope.params;
                var id = items.length ? items[0].id + 1 : 1;

                var balancerate = {
                    id: id,
                    balance_rate_id: '',
                    appname: '',
                    BUGUID: '',
                    BUName: '',
                    rate: '',
                    editMode: 1
                };
                $scope.params.unshift(balancerate);
            };

            $scope.cancelBalanceRate = function (record,idx) {
                if (record.editMode == 1) {
                    $scope.params.splice(idx, 1);
                } else {
                    angular.extend(record, record.orginalData);
                    record.editMode = 0;
                    record.orginalData = '';
                }
            };
            
            $scope.editBalanceRate = function (record) {
                record.orginalData = angular.copy(record);
                record.editMode = 2;
            };

            $scope.getBalanceRateInfo();
            $scope.getBalanceRate();
        }])
})